const app = getApp();
const util = require('../../static/js/util.js');
const {
  phoneExp,
} = require('../../../common/utils/util.js');

Page({
  data: {
    isPassword: true,
    isAccount: true,
    isCode: true,
    account: '',
    password: '',
    phone: '',
    code: '',
    time: 60,
    isImmediately: false,
    imgUrl: util.getImgUrl(),
  },
  isSendCode: true,
  isLogin: true,
  onShow() {
    if (!wx.getStorageSync('userInfo').sessionid) {
      util.getUserInfo();
    }
  },
  bindInputValue(e) {
    const id = e.currentTarget.dataset.id;
    const m = {};
    m[id] = e.detail.value;
    this.setData(m);
  },

  changePassword() {
    this.setData({
      isPassword: !this.data.isPassword,
    });
  },
  changeLogin(e) {
    this.setData({
      isAccount: e.currentTarget.dataset.name === 'account' ? true : false,
    });
  },
  sendCode() {
    let {
      phone,
      isAccount,
      time,
    } = this.data;
    let countDown = {};
    if (!isAccount && phone === '') {
      wx.showToast({
        title: `手机号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!isAccount && !phoneExp.mobile.test(phone)) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'none',
      });
      return;
    }
    if (!this.isSendCode) return;
    this.isSendCode = false;
    util.request({
      url: 'entry/wxapp/user.authentication.getcode',
      data: {
        mobile: phone,
      },
      success: (res) => {
        console.log(res);
        this.setData({
          isCode: false,
        });
        let count = setInterval(() => {
          time = time - 1;
          countDown.time = time;
          if (time === 0) {
            clearInterval(count); // 清除倒计时
            countDown.time = 60; // 设置倒计时时间
            countDown.isCode = true;
          }
          this.setData(countDown);
        }, 1000);
      },
      complete: () => {
        this.isSendCode = true;
      },
    });
  },
  goAccountLogin(e) {
    const {
      account,
      password,
    } = this.data;
    const currentPage = wx.getStorageSync('currentPage');
    if (account === '') {
      wx.showToast({
        title: `账号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (password === '') {
      wx.showToast({
        title: `密码不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (account === '') {
      wx.showToast({
        title: `手机号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!phoneExp.mobile.test(account)) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'none',
      });
      return;
    }
    if (!this.isLogin) return;
    this.isLogin = false;
    util.request({
      url: 'entry/wxapp/user.authentication.login',
      data: {
        mobile: account,
        password: password,
      },
      success: (res) => {
        if (currentPage && currentPage.route && currentPage.route !== 'ws_party_const/pages/login/login') {
          app.util.navigateTo(currentPage.route, currentPage.options, true);
        } else {
          wx.switchTab({
            url: '/ws_party_const/pages/index/index',
          });
        } 
      },
      complete: () => {
        this.isLogin = true;
        this.setData({
          isImmediately: false,
        });
      },
    });
  },
  goMobileLogin(e) {
    const {
      phone,
      code,
    } = this.data;
    const currentPage = wx.getStorageSync('currentPage');
    if (phone === '') {
      wx.showToast({
        title: `手机号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!phoneExp.mobile.test(phone)) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'none',
      });
      return;
    }
    if (code === '') {
      wx.showToast({
        title: `验证码不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!this.isLogin) return;
    this.isLogin = false;
    util.request({
      url: 'entry/wxapp/user.authentication.login2',
      data: {
        mobile: phone,
        pwdCode: code,
      },
      success: (res) => {
        if (currentPage && currentPage.route && currentPage.route !== 'ws_party_const/pages/login/login') {
          app.util.navigateTo(currentPage.route, currentPage.options, true);
        } else {
          wx.switchTab({
            url: '/ws_party_const/pages/index/index',
          });
        }
      },
      complete: () => {
        this.isLogin = true;
      },
    });
  },
  getAccount() {
    util.request({
      url: 'entry/wxapp/user.authentication.getdemoaccount',
      data: {},
      success: (res) => {
        this.setData({
          account: res.data.mobile,
          password: res.data.pwd,
          isImmediately: true,
        }, () => {
          setTimeout(() => {
            this.goAccountLogin();
          }, 1000);
        });
      },
    });
  },
  onUnload() {
    wx.removeStorageSync('adsTime');
    wx.removeStorageSync('currentPage');
  },
});